From 73049b38688effa33089f95c31b2563b7ca8f54d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 21 Jul 2014 19:18:23 -0700 Subject: [PATCH] Move git macros to using `arg()` --- src/cargo/core/source.rs | 12 ++++++ src/cargo/sources/git/utils.rs | 78 ++++++++++++++-------------------- tests/support/mod.rs | 1 - 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/src/cargo/core/source.rs b/src/cargo/core/source.rs index c331c0245..84c7faca2 100644 --- a/src/cargo/core/source.rs +++ b/src/cargo/core/source.rs @@ -1,6 +1,7 @@ use std::fmt; use std::fmt::{Show, Formatter}; use std::hash; +use std::c_str::CString; use serialize::{Decodable, Decoder, Encodable, Encoder}; use url::Url; @@ -105,6 +106,17 @@ impl Location { } } +impl<'a> ToCStr for &'a Location { + fn to_c_str(&self) -> CString { + match **self { + Local(ref p) => p.to_c_str(), + Remote(ref u) => u.to_string().to_c_str(), + } + } + + unsafe fn to_c_str_unchecked(&self) -> CString { self.to_c_str() } +} + impl Show for SourceId { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match *self { diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 0d8c6e90b..1e0e40369 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -4,7 +4,7 @@ use std::io::{UserDir}; use std::io::fs::{mkdir_recursive,rmdir_recursive}; use serialize::{Encodable,Encoder}; -use core::source::{Location, Local, Remote}; +use core::source::Location; use util::{CargoResult, ChainError, ProcessBuilder, process, human}; #[deriving(PartialEq,Clone,Encodable)] @@ -40,23 +40,15 @@ impl Show for GitReference { macro_rules! git( - ($config:expr, $str:expr, $($rest:expr),*) => ( - try!(git_inherit(&$config, format!($str, $($rest),*))) - ); - - ($config:expr, $str:expr) => ( - try!(git_inherit(&$config, format!($str))) - ); + ($config:expr, $($arg:expr),+) => ( + try!(git_inherit(&$config, process("git")$(.arg($arg))*)) + ) ) macro_rules! git_output( - ($config:expr, $str:expr, $($rest:expr),*) => ({ - try!(git_output(&$config, format!($str, $($rest),*))) - }); - - ($config:expr, $str:expr) => ({ - try!(git_output(&$config, format!($str))) - }); + ($config:expr, $($arg:expr),*) => ({ + try!(git_output(&$config, process("git")$(.arg($arg))*)) + }) ) macro_rules! errln( @@ -147,7 +139,7 @@ impl GitRemote { } pub fn has_ref(&self, path: &Path, reference: S) -> CargoResult<()> { - git_output!(*path, "rev-parse {}", reference.as_slice()); + git_output!(*path, "rev-parse", reference.as_slice()); Ok(()) } @@ -166,8 +158,8 @@ impl GitRemote { } fn fetch_into(&self, path: &Path) -> CargoResult<()> { - Ok(git!(*path, "fetch --force --quiet --tags {} \ - refs/heads/*:refs/heads/*", self.fetch_location())) + Ok(git!(*path, "fetch", "--force", "--quiet", "--tags", + &self.location, "refs/heads/*:refs/heads/*")) } fn clone_into(&self, path: &Path) -> CargoResult<()> { @@ -175,15 +167,8 @@ impl GitRemote { try!(mkdir_recursive(path, UserDir)); - Ok(git!(dirname, "clone {} {} --bare --no-hardlinks --quiet", - self.fetch_location(), path.display())) - } - - fn fetch_location(&self) -> String { - match self.location { - Local(ref p) => p.display().to_string(), - Remote(ref u) => u.to_string(), - } + Ok(git!(dirname, "clone", &self.location, path, "--bare", + "--no-hardlinks", "--quiet")) } } @@ -204,7 +189,7 @@ impl GitDatabase { } pub fn rev_for(&self, reference: S) -> CargoResult { - Ok(git_output!(self.path, "rev-parse {}", reference.as_slice())) + Ok(git_output!(self.path, "rev-parse", reference.as_slice())) } } @@ -247,8 +232,8 @@ impl GitCheckout { })); } - git!(dirname, "clone --no-checkout --quiet {} {}", - self.get_source().display(), self.location.display()); + git!(dirname, "clone", "--no-checkout", "--quiet", + self.get_source(), &self.location); Ok(()) } @@ -273,38 +258,39 @@ impl GitCheckout { // https://www.kernel.org/pub/software/scm/git/docs/RelNotes-1.7.3.txt // // In this case we just use `origin` here instead of the database path. - git!(self.location, "fetch --force --quiet origin"); - git!(self.location, "fetch --force --quiet --tags origin"); + git!(self.location, "fetch", "--force", "--quiet", "origin"); + git!(self.location, "fetch", "--force", "--quiet", "--tags", "origin"); try!(self.reset(self.revision.as_slice())); Ok(()) } - fn reset(&self, revision: T) -> CargoResult<()> { - Ok(git!(self.location, "reset -q --hard {}", revision)) + fn reset(&self, revision: &str) -> CargoResult<()> { + Ok(git!(self.location, "reset", "-q", "--hard", revision)) } fn update_submodules(&self) -> CargoResult<()> { - Ok(git!(self.location, "submodule update --init --recursive --quiet")) + Ok(git!(self.location, "submodule", "update", "--init", + "--recursive", "--quiet")) } } -fn git(path: &Path, str: &str) -> ProcessBuilder { - debug!("Executing git {} @ {}", str, path.display()); +fn git(path: &Path, cmd: ProcessBuilder) -> ProcessBuilder { + debug!("Executing {} @ {}", cmd, path.display()); - process("git").args(str.split(' ').collect::>().as_slice()) - .cwd(path.clone()) + cmd.cwd(path.clone()) } -fn git_inherit(path: &Path, str: String) -> CargoResult<()> { - git(path, str.as_slice()).exec().chain_error(|| { - human(format!("Executing `git {}` failed", str)) +fn git_inherit(path: &Path, cmd: ProcessBuilder) -> CargoResult<()> { + let cmd = git(path, cmd); + cmd.exec().chain_error(|| { + human(format!("Executing {} failed", cmd)) }) } -fn git_output(path: &Path, str: String) -> CargoResult { - let output = try!(git(path, str.as_slice()).exec_with_output() - .chain_error(|| - human(format!("Executing `git {}` failed", str)))); +fn git_output(path: &Path, cmd: ProcessBuilder) -> CargoResult { + let cmd = git(path, cmd); + let output = try!(cmd.exec_with_output().chain_error(|| + human(format!("Executing {} failed", cmd)))); Ok(to_str(output.output.as_slice()).as_slice().trim_right().to_string()) } diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 4ecbc22a0..132d223d6 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -112,7 +112,6 @@ impl ProjectBuilder { process(program) .cwd(self.root()) .env("HOME", Some(paths::home().display().to_string().as_slice())) - .extra_path(cargo_dir()) } pub fn cargo_process(&self, program: &str) -> ProcessBuilder { -- 2.30.2